<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Order_m extends FOX_Model
{

    function __construct()
    {
        parent::__construct();
        $this->config->load('userset');//用户积分
    }

    //找出最新商品订单
    public function get_goods_order_by_good_id_news($good_id, $uid)
    {
        $query = $this->db->select_max('order_add_time')->get_where('shop_orders', ['good_id' => $good_id, 'user_id' => $uid]);
        return $query->row_array();
    }

    /**
     * @param $order_id
     * @return mixed
     * 查询单条
     */
    public function get_shop_orders_by_id($order_id)
    {
        $query = $this->db
            ->select('*')
            ->get_where('shop_orders', ['order_id' => $order_id]);
        return $query->row_array();
    }

    /**
     * @param $order_id
     * @param $data
     * @return bool
     * 修改订单信息
     */
    public function sava_shop_orders($order_id, $data)
    {

        $this->db->where('order_id', $order_id);
        $this->db->update('shop_orders', $data);
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    /**
     * @param $data
     * @return bool
     * 新增订单
     */
    public function add_shop_orders($data)
    {

        $this->db->insert('shop_orders', $data);
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有订单
     */
    public function get_all_shop_orders()
    {
        $this->db->select('*');
        $this->db->from('shop_orders');
        $this->db->join('shop_users', 'shop_orders.shop_id = shop_users.shop_id', 'left');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    public function sucsess_order($order_id)
    {

        $data = $this->get_shop_orders_by_id($order_id);

        $this->db->trans_start();
        // 更新订单状态为成功
        $this->db->set('order_status', 2, false)->where('order_id', $order_id)->update('shop_orders');
        $this->db->set('order_add_time', time(), false)->where('order_id', $order_id)->update('shop_orders');
        // 商品库存减少
        $this->db->set('good_stock', 'good_stock-1', false)->where('good_id', $data['good_id'])->update('shop_goods');
        $this->db->set('good_orders', 'good_orders+1', false)->where('good_id', $data['good_id'])->update('shop_goods');
        // 用户积分增加+积分明细
        $this->db->set('user_credit', 'user_credit+' . $this->config->item('credit_jianlou'), false)->where('user_id', $data['user_id'])->update('users');
        $credit = [
            "user_id"     => $data['user_id'],
            "credit_num"  => $this->config->item('credit_jianlou'),
            "credit_type" => 3,
            "credit_time" => time(),
        ];
        $this->db->insert('credits', $credit);
        $this->db->trans_complete();

        if ($this->db->trans_status() === false) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 订单待确认
     * @param $data
     */
    public function wait_order($data, $type = 0)
    {

        $this->db->trans_start();
        if ($type == 0) {
            $this->db->insert('shop_orders', $data);
            $order_id = $this->db->insert_id();
            $this->db->set('user_money', 'user_money-' . $data['order_price'], false)->where('user_id', $data['user_id'])->update('users');
            $money = [
                "user_id"    => $data['user_id'],
                "money_num"  => $data['order_price'],
                "money_item" => $data['good_id'],
                "money_type" => 2,
                "money_time" => time(),
            ];
            $this->db->insert('moneys', $money);
            $this->db->set('order_pay', '3', false)->where('order_id', $order_id)->update('shop_orders');
            $this->db->trans_complete();
            return $order_id;
        }
        if ($type == 2) {

            // 成功逻辑
            // 更新订单状态为成功
            $this->db->set('order_status', $type, false)->where('order_id', $data['order_id'])->update('shop_orders');
            // 商品库存减少
            $this->db->set('good_stock', 'good_stock-1', false)->where('good_id', $data['good_id'])->update('shop_goods');
            // 订单数加一
            $this->db->set('good_orders', 'good_orders+1', false)->where('good_id', $data['good_id'])->update('shop_goods');
            // 用户积分增加+积分明细
            $this->db->set('user_credit', 'user_credit+' . $this->config->item('credit_jianlou'), false)->where('user_id', $data['user_id'])->update('users');
            $credit = [
                "user_id"     => $data['user_id'],
                "credit_num"  => $this->config->item('credit_jianlou'),
                "credit_type" => 3,
                "credit_time" => time(),
            ];
            $this->db->insert('credits', $credit);
            // 更新捡漏成功的时间
            $this->db->set('order_update_time', time(), false)->where('order_id', $data['order_id'])->update('shop_orders');
            //商家操作确认
            $this->db->set('order_shop_ok', 1, false)->where('order_id', $data['order_id'])->update('shop_orders');
            $this->db->trans_complete();
        }
        if ($type == 1) {
            // 失败，回滚
            $this->db->set('order_status', $type, false)->where('order_id', $data['order_id'])->update('shop_orders');
            $this->db->set('user_money', 'user_money+' . $data['order_price'], false)->where('user_id', $data['user_id'])->update('users');
            $money = [
                "user_id"    => $data['user_id'],
                "money_num"  => $data['order_price'],
                "money_item" => $data['good_id'],
                "money_type" => 1,
                "money_time" => time(),
            ];
            //商家操作确认
            $this->db->set('order_shop_ok', 1, false)->where('order_id', $data['order_id'])->update('shop_orders');
            $this->db->insert('moneys', $money);
            $this->db->trans_complete();
        }

        if ($this->db->trans_status() === false) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @return mixed
     * 查询最进成交的10条订单
     */
    public function get_ten_shop_orders()
    {
        $field = "shop_orders.user_id,users.user_avatar,users.user_name,users.user_uname,shop_orders.order_update_time";
        $this->db->select($field);
        $this->db->from('shop_orders');
        $this->db->join('users', 'shop_orders.user_id = users.user_id', 'left');
        $this->db->where("shop_orders.order_status", 2);
        $this->db->limit(8);
        $this->db->order_by('shop_orders.order_id', "DESC");
        // $this->db->join('shop_goods as g','o.good_id = g.good_id','left');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    /**
     * @return mixed
     * 查询最进成交的10条订单
     */
    public function get_ten_shop_goods()
    {
        $field = "shop_goods.shop_id,shop_goods.user_id,users.user_avatar,users.user_name,users.user_uname,shop_goods.good_add_time";
        $this->db->select($field);
        $this->db->from('shop_goods');
        $this->db->join('users', 'shop_goods.user_id = users.user_id', 'left');
        $this->db->limit(3);
        $this->db->order_by('shop_goods.good_id', "DESC");
        // $this->db->join('shop_goods as g','o.good_id = g.good_id','left');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

//后台查询所有的订单
    public function get_all_orders($start, $limit, $uid = 0)
    {
        $this->db->select('*');
        $this->db->from('shop_orders');
        if (!empty($uid)) {
            $this->db->where("user_id", $uid);
        }
        $this->db->where("order_pay", 1);
        $this->db->limit($limit, $start);
        $this->db->order_by('order_id', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_recent_one_record($order_id = '')
    {
        $field = "shop_orders.user_id,users.user_avatar,users.user_name,users.user_uname,shop_orders.order_update_time";
        $this->db->select($field);
        $this->db->from('shop_orders');
        $this->db->join('users', 'shop_orders.user_id = users.user_id', 'left');
        $this->db->where("shop_orders.order_id", $order_id);
        $query = $this->db->get();
        return $query->row_array();
    }

    //查询已发货的订单
    public function get_delivers($start, $limit)
    {
        $this->db->select('*')->from('shop_orders')->where(['order_status' => 2, 'order_deliver' => 2])->limit($limit, $start)->order_by('order_id', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }

    //查询未发货的订单
    public function get_delivering($start, $limit)
    {
        $this->db->select('*')->from('shop_orders')->where(['order_status' => 2, 'order_deliver' => 1])->limit($limit, $start)->order_by('order_id', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }

    //查询所有需要未发货的订单的总数
    public function count_all_ing()
    {
        $this->db->select('order_id');
        $this->db->from('shop_orders');
        $this->db->where(['order_status' => 2, 'order_deliver' => 1]);
        $total = $this->db->count_all_results();
        return $total;
    }

    //查询所有已发货的订单的总数
    public function count_all_ings()
    {
        $this->db->select('order_id');
        $this->db->from('shop_orders');
        $this->db->where(['order_status' => 2, 'order_deliver' => 2]);
        $total = $this->db->count_all_results();
        return $total;
    }

    //商家端获取自己的订单
    public function get_shop_orders($page = 0, $limit = 4, $shopid, $type = 1000)
    {
        $start = $page * $limit;
        $this->db->select('shop_orders.*,shop_goods.good_title,shop_goods.good_cover');
        $this->db->from('shop_orders');
        $this->db->join('shop_goods', 'shop_orders.good_id = shop_goods.good_id', 'left');
        $this->db->where(['shop_orders.order_status' => 2, 'shop_orders.shop_id' => $shopid]);
        if ($type <> 1000) {
            $this->db->where('order_deliver', $type);
        }
        $this->db->order_by('shop_orders.order_id', 'DESC');
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

    //商家端获取自己的所有订单
    public function get_shop_orders_all($page = 0, $limit = 4, $shopid)
    {
        $start = $page * $limit;
        $this->db->select('shop_orders.*,shop_goods.good_title,shop_goods.good_cover');
        $this->db->from('shop_orders');
        $this->db->join('shop_goods', 'shop_orders.good_id = shop_goods.good_id', 'left');
        $this->db->where(['shop_orders.shop_id' => $shopid]);
        $this->db->order_by('shop_orders.order_id', 'DESC');
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

    //查询所有订单的总数
    public function get_shop_orders_count_all($shopid, $type = 1000)
    {
        $this->db->select('order_id');
        $this->db->from('shop_orders');
        if ($type <> 1000) {
            $this->db->where('order_deliver', $type);
        }
        $this->db->where(['shop_id' => $shopid]);
        $total = $this->db->count_all_results();
        return $total;
    }

    //查询所有已发货的订单的总数
    public function get_shop_orders_count($shopid, $type = 1000)
    {
        $this->db->select('order_id');
        $this->db->from('shop_orders');
        if ($type <> 1000) {
            $this->db->where('order_deliver', $type);
        }
        $this->db->where(['order_status' => 2, 'shop_id' => $shopid]);
        $total = $this->db->count_all_results();
        return $total;
    }

    //删除失败订单
    public function del_order_by_id($id)
    {
        $this->db->where('order_id', $id)->delete('shop_orders');
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    /**
     * 个人某商品订单总金额
     * @param $uid
     * @param $good_id
     * @return mixed
     */
    public function personal_order_total_fee($uid, $good_id)
    {
        $this->db->select("sum(order_price) as total_fee");
        $this->db->from('shop_orders');
        $this->db->where("user_id", $uid);
        $this->db->where("good_id", $good_id);
        $this->db->where("order_status", 2);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function personal_order_total_count($uid, $good_id)
    {
        $this->db->select("order_id");
        $this->db->from('shop_orders');
        $this->db->where("user_id", $uid);
        $this->db->where("good_id", $good_id);
        $this->db->where("order_status", 2);
        $total = $this->db->count_all_results();
        return $total;
    }

    /**
     * 获取下级某商品订单
     * @param $good_id
     * @param $ids 下级数据
     */
    public function get_sub_good_order($good_id, $ids)
    {
        $this->db->select("order_id");
        $this->db->from('shop_orders');
        $this->db->where_in("user_id", $ids);
        $this->db->where("good_id", $good_id);
        $this->db->where("order_status", 2);
        $total = $this->db->count_all_results();
        return $total;
    }

    public function get_orders_with_voucher_by_uid($uid)
    {
        $this->db->select('shop_orders.*,');
        $this->db->from('shop_orders');
        $this->db->join('shop_goods', 'shop_orders.good_id = shop_goods.good_id', 'left');
        $this->db->where(['shop_orders.order_status' => 2, 'shop_orders.user_id' => $uid, 'shop_goods.good_voucher' => 1]);
        $this->db->order_by('shop_orders.order_id', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function voucher_order($order)
    {

        $this->db->insert('shop_orders', $order);
        $order_id = $this->db->insert_id();
        // 商品库存减一
        $this->db->set('good_stock', 'good_stock-1', false)->where('good_id', $order['good_id'])->update('shop_goods');
        // 订单数加一
        $this->db->set('good_orders', 'good_orders+1', false)->where('good_id', $order['good_id'])->update('shop_goods');
        // 免单券状态改为已使用
        $this->db->set('voucher_lock', 1, false)->where('good_id', $order['good_id'])->update('free_vouchers');
        return $order_id;
    }

    public function create_order($data)
    {

        $this->db->insert('shop_orders', $data);
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    /**
     * @param $order_id
     * @return mixed
     * 查询单条
     */
    public function get_shop_orders_by_order_no($order_no)
    {
        $query = $this->db
            ->select('*')
            ->get_where('shop_orders', ['order_no' => $order_no]);
        return $query->row_array();
    }

    public function sava_shop_orders_by_no($order_no, $data)
    {

        $this->db->where('order_no', $order_no);
        $this->db->update('shop_orders', $data);
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    public function get_all_orders_mobile($start, $limit, $uid = 0)
    {
        $this->db->select('shop_orders.*,shop_goods.good_title,shop_goods.good_category,shop_goods.good_price,shop_goods.good_memo');
        $this->db->from('shop_orders');
        $this->db->join('shop_goods', 'shop_orders.good_id = shop_goods.good_id', 'left');
        if (!empty($uid)) {
            $this->db->where("shop_orders.user_id", $uid);
        }
        $this->db->where("shop_orders.order_status >", 0);
        $this->db->limit($limit, $start);
        $this->db->order_by('shop_orders.order_id', 'DESC');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    public function get_shop_orders_count_by_uid($uid)
    {
        $this->db->select('order_id');
        $this->db->from('shop_orders');
        $this->db->where("user_id", $uid);
        $this->db->where("order_status >", 0);
        $total = $this->db->count_all_results();
        return $total;
    }

}
